<template>
  <div class="search-popup">
    <van-search
      v-model="searchVal"
      show-action
      :placeholder="placeholderVal"
      @search="onSearch"
      @cancel="onCancel"
      @input="onInput"
    />
    <!-- 输入框内容发生变化时触发这个函数 -->
    <HistoryHot
      v-if="blockShow == 1"
      :historyListData="historyListData"
      :hotKeywordListData="hotKeywordListData"
      @tagClick="tagClick"
    />
    <SearchTipsList
      v-else-if="blockShow == 2"
      :SearchTipsListData="SearchTipsListData"
      @cellClick="cellClick"
    />
    <SearchProducts
      v-else
      :filterCategory="filterCategory"
      :goodsList="goodsList"
      @categoryChange="categoryChange"
      @priceChange="priceChange"
    />
  </div>
</template>

<script>
import HistoryHot from "@/components/HistoryHot.vue";
import SearchTipsList from "@/components/SearchTipsList.vue";
import SearchProducts from "@/components/SearchProducts.vue";
import {
  GetSearchPopupData,
  GetSearchTipsListData,
  GetGoodsListData,
} from "@/request/api";
export default {
  components: { HistoryHot },
  data() {
    return {
      // 用户输入的信息
      searchVal: "",
      // 默认展示的搜索信息
      placeholderVal: "",
      // 1表示历史记录和热门搜索
      blockShow: 1,
      // 历史记录列表数据
      historyListData: [],
      // 热门搜索列表数据
      hotKeywordListData: [],
      // 搜索实时提示的列表数据
      SearchTipsListData: [],
      // 展示是全部还是分类的东西
      filterCategory: [],
      // 展示当前所有的商品
      goodsList: [],
      // 价格排序（由低到高或者由高到低）
      order: "desc",
      // 分类id
      categoryId: 0,
      // 默认的搜索方式，是id还是price
      sort: "id",
    };
  },
  created() {
    GetSearchPopupData().then((res) => {
      console.log("搜索框获得的信息:");
      console.log(res);
      if (res.errno == 0) {
        this.placeholderVal = res.data.defaultKeyword.keyword;
        this.historyListData = res.data.historyKeywordList;
        this.hotKeywordListData = res.data.hotKeywordList;
      }
    });
  },
  methods: {
    cellClick(value) {
      this.searchVal = value;
      this.onSearch(value);
    },
    tagClick(value) {
      // 改变上面搜索的值 就可以在做priceChange排序的时候 按照关键词排序
      this.searchVal = value;
      this.onSearch(value);
    },
    categoryChange(value) {
      this.categoryId = value;
      this.onSearch(this.searchVal);
    },
    priceChange(value) {
      // value 是 desc 和 asc
      this.order = value;
      this.sort = "price";
      this.onSearch(this.searchVal);
    },
    onSearch(val) {
      this.blockShow = 3;
      // console.log("用户输入的值", val);
      GetGoodsListData({
        keyword: val,
        page: 1,
        size: 10,
        order: this.order,
        categoryId: this.categoryId,
        sort: this.sort,
      }).then((res) => {
        if (res.errno == 0) {
          // console.log("res.data", res.data);
          // 方法一
          // let { filterCategory, goodsList } = res.data;
          // let newArr = JSON.parse(
          //   JSON.stringify(filterCategory)
          //     .replace(/name/g, "text")
          //     .replace(/id/g, "value")
          // );
          // this.filterCategory = newArr;
          // 方法二
          let newArr = JSON.parse(
            JSON.stringify(res.data.filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          this.filterCategory = newArr;
          // this.filterCategory = res.data.filterCategory;
          this.goodsList = res.data.goodsList;
        }
      });
    },
    onCancel() {
      this.$router.go(-1);
      this.$store.commit("changeIsShowPopupShadow", false);
    },
    onInput(val) {
      this.blockShow = 2;
      // 用户在输入框输入的数据值 console.log(val);
      GetSearchTipsListData({ keyword: val }).then((res) => {
        if (res.errno == 0) {
          // 搜索到的数据 console.log(res.data);
          this.SearchTipsListData = res.data;
        }
        // console.log(keyword:val)
      });
    },
  },
  components: {
    HistoryHot,
    SearchTipsList,
    SearchProducts,
  },
};
</script>

<style>
.search-popup {
  z-index: 100;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  /* background-color: rgba(0, 0, 0, 0.5); */
  background-color: #efefef;
}
</style>